package org.springside.core.dao;

import java.util.List;

/**
 * springside自带分页类
 * @author user
 *
 */
@SuppressWarnings({"rawtypes"})
public class Page
{
  public static final int PAGESIZE = 10;
  private int pageSize = 10;
  private int[] indexs;
  private int startIndex;
  private long totalCount;
  private List items;
  private int currentPage;
  private int[] pages;
  private boolean hasPreviousPage;
  private boolean hasNextPage;
  private int lastIndex;

  protected Page()
  {
  }

  public Page(List items, long totalCount)
  {
    setPageSize(items.size());
    setTotalCount(totalCount);
    setItems(items);
    setStartIndex(0L);
  }

  public Page(List items, long totalCount, int startIndex) {
    setPageSize(items.size());
    setTotalCount(totalCount);
    setItems(items);
    setStartIndex(startIndex);
  }

  public Page(List items, long totalCount, int pageSize, int startIndex) {
    setPageSize(pageSize);
    setTotalCount(totalCount);
    setItems(items);
    setStartIndex(startIndex);
  }

  public static int getPAGESIZE()
  {
    return 10;
  }

  public int[] getIndexs()
  {
    return this.indexs;
  }

  public void setIndexs(int[] indexs)
  {
    this.indexs = indexs;
  }

  public int getNextIndex()
  {
    int nextIndex = getStartIndex() + this.pageSize;
    if (nextIndex >= this.totalCount) {
      return getStartIndex();
    }
    return nextIndex;
  }

  public void setNextIndex(int nextIndex)
  {
  }

  public int getPageSize()
  {
    return this.pageSize;
  }

  public void setPageSize(int pageSize)
  {
    this.pageSize = pageSize;
  }

  public int getPreviousIndex()
  {
    System.out.println("in getPreviousIndex");
    int previousIndex = getStartIndex() - this.pageSize;
    if (previousIndex < 0) {
      return 0;
    }
    return previousIndex;
  }

  public void setPreviousIndex(int previousIndex)
  {
  }

  public int getStartIndex()
  {
    return this.startIndex;
  }

  public void setStartIndex(long startIndex)
  {
    if (this.totalCount <= 0L)
      this.startIndex = 0;
    else if (startIndex >= this.totalCount)
      this.startIndex = this.indexs[(this.indexs.length - 1)];
    else if (startIndex < 0L)
      this.startIndex = 0;
    else
      this.startIndex = this.indexs[(int)(startIndex / this.pageSize)];
  }

  public long getTotalCount()
  {
    return this.totalCount;
  }

  public void setTotalCount(long totalCount)
  {
    if (totalCount > 0L)
    {
      this.totalCount = totalCount;

      int count = (int)(totalCount / this.pageSize);
      if (totalCount % this.pageSize > 0L)
        count++;
      this.indexs = new int[count];

      for (int i = 0; i < count; i++) {
        this.indexs[i] = (this.pageSize * i);
      }
      setLastIndex(this.indexs[(this.indexs.length - 1)]);
    } else {
      this.totalCount = 0L;
      this.indexs = new int[0];
      setLastIndex(0);
    }
  }

  public List getItems()
  {
    return this.items;
  }

  public void setItems(List items)
  {
    this.items = items;
  }

  public int[] getPages() {
    this.pages = new int[getIndexs().length];
    for (int i = 0; i < this.pages.length; i++) {
      this.pages[i] = (i + 1);
    }
    return this.pages;
  }

  public void setPages(int[] pages) {
    this.pages = pages;
  }

  public int getCurrentPage() {
    for (int i = 0; i < getIndexs().length; i++) {
      if (getStartIndex() == getIndexs()[i]) {
        setCurrentPage(i + 1);
      }
    }
    return this.currentPage;
  }

  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }

  public int getLastIndex() {
    return this.lastIndex;
  }

  public void setLastIndex(int lastIndex) {
    this.lastIndex = lastIndex;
  }

  public boolean hasNextPage() {
    if (getCurrentPage() == getPages().length)
      setHasNextPage(false);
    else setHasNextPage(true);
    return this.hasNextPage;
  }

  public boolean getHasNextPage() {
    if (getCurrentPage() == getPages().length)
      setHasNextPage(false);
    else setHasNextPage(true);
    return this.hasNextPage;
  }
  public void setHasNextPage(boolean hasNextPage) {
    this.hasNextPage = hasNextPage;
  }

  public boolean hasPreviousPage() {
    if (getCurrentPage() == 1)
      setHasPreviousPage(false);
    else setHasPreviousPage(true);
    return this.hasPreviousPage;
  }

  public boolean getHasPreviousPage() {
    if ((getCurrentPage() == 0) || (getCurrentPage() == 1))
      setHasPreviousPage(false);
    else setHasPreviousPage(true);
    return this.hasPreviousPage;
  }
  public void setHasPreviousPage(boolean hasPreviousPage) {
    this.hasPreviousPage = hasPreviousPage;
  }

  public String toString() {
    StringBuilder buff = new StringBuilder();
    String sf = "[ ";
    for (int s : getIndexs()) {
      sf = sf + s + " ";
    }
    sf = sf + "]";

    String spg = "[";
    for (int j : getPages()) {
      spg = spg + j + " ";
    }
    spg = spg + "]";
    buff.append("当前Page对象信息如下").append("\n")
      .append("分页索引的数组：" + sf).append("\n")
      .append("当前分页索引:" + getStartIndex()).append("\n")
      .append("总结果数：" + getTotalCount()).append("\n")
      .append("下一页索引:" + getNextIndex()).append("\n")
      .append("上一页索引:" + getPreviousIndex()).append("\n")
      .append("当前页码数：" + getCurrentPage()).append("\n")
      .append("页码列表：" + spg);
    return buff.toString();
  }
}